package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.PlaceOfficerItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class PlaceOfficerThread extends BasicThread {

	public interface PlaceOfficerThreadCallback {
		public void OnPlaceOfficerCompleted(boolean isNetworkConntected, boolean results
				, String message);
		public void OnPlaceOfficerError(ErrorItem errorItem);
	}

	private String url;
	private PlaceOfficerThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String codeNm, offCd;
	
	public PlaceOfficerThread(Context context,
			PlaceOfficerThreadCallback callback, String codeNm, String offCd
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.codeNm = codeNm;
		this.offCd = offCd;
	}

	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHNM, codeNm);
		String param = "";
		param = parameterReturn(Const.REQ011, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnPlaceOfficerCompleted(false, false, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnPlaceOfficerError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			Log.i("result","result = " + result);
			ArrayList<PlaceOfficerItem> arrOfficerItem = new ArrayList<PlaceOfficerItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				appStatus.setArrPlaceOfficerItem(arrOfficerItem);
				callback.OnPlaceOfficerError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				Log.i("arrayLogin","arrayLogin = " + arrayLogin);
				PlaceOfficerItem officerItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject prod = arrayLogin.getJSONObject(i);
					
					officerItem = new PlaceOfficerItem();
					officerItem.setAllInsttNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, PlaceOfficerItem.ALLINSTTNM)));
					officerItem.setUserAcnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, PlaceOfficerItem.USERACNT)));
					officerItem.setUserNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, PlaceOfficerItem.USERNM)));
					officerItem.setUserSn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, PlaceOfficerItem.USERSN)));
					
					arrOfficerItem.add(officerItem);
				}
				appStatus.setArrPlaceOfficerItem(arrOfficerItem);
				callback.OnPlaceOfficerCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();
	
		url = Const.MAIN_URL;
		post = ParameterUtil.getFirstPlaceOfficerParam(codeNm, offCd);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);
		Log.i("url","url = " + url + post);
		Log.i("url","json = " + json);*/
		
	
		/*try {		
			
		} catch (JSONException e) {
		}*/
	}
}
